<%@ page language="java" contentType="text/html; charset=UTF-8" %>
<%@ page import="com.test.SvnListTempStatic" %>
<%@ page import="com.component.CustomInfo" %>
<%@ page import="java.util.*" %>
<%@ page import="com.component.CustomInfo" %>
<%@ page import="ch.ethz.ssh2.Connection" %>
<%@ page import="ch.ethz.ssh2.Session" %>
<%@ page import="ch.ethz.ssh2.StreamGobbler" %>
<%@ page import="java.io.*" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="sun.java2d.cmm.CMSManager" %>
<%@ page import="ch.ethz.ssh2.ConnectionInfo" %>
<%@ page import="java.net.SocketTimeoutException" %>

<%!
    private static String readMessage(InputStream inputStream, String encoding) throws IOException {
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, encoding))) {
            StringBuilder message = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                message.append("<br>").append(line).append(System.lineSeparator());
            }
            return message.length() > 0 ? message.toString() : null;
        }
    }
    public static String null2String(String s) {
        return s == null ? "" : s;

    }
%>
<%
    String title = "";

    int showRow=1000;
    //ec日志
    String EcologyLogcmd = "tail -n "+showRow+" notes   /weaver/ecology/log/ecology";
    //集成日志
    String IntergationLogcmd = "tail -n "+showRow+" notes  /weaver/ecology/log/integration/integration.log";

    Date date = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    String today = sdf.format(date);

    //安全包
    String systemRunInfo = "systemRunInfo" + today + ".log";
    String systemRunInfoLogcmd = "tail -n "+showRow+" notes  /weaver/ecology/WEB-INF/securitylog/" + systemRunInfo;

    //resin日志
    String resinJVMLogCmd = "tail -n "+showRow+" notes /weaver/Resin/log/jvm-app-0.log";
    String resinStderrLogCmd = "tail -n "+showRow+" notes /weaver/Resin/log/stderr.log";

    String resin3JVMLogCmd = "tail -n "+showRow+" notes /weaver/Resin/log/jvm-default.log";
    String showMessage = "";


    String logtype = request.getParameter("logtype");//日志类型
    String cmd = "";
    String encoding = "gb2312";
    if (logtype.equals("1")) {
        cmd = EcologyLogcmd;
        title = "ecology";
    } else if (logtype.equals("2")) {
        cmd = IntergationLogcmd;
        title = "integrtion" + today;
    } else if (logtype.equals("3")) {
        cmd = systemRunInfoLogcmd;
        title = "systemRunInfo";
    } else if (logtype.equals("4")) {
        cmd = resinJVMLogCmd;
        title = "JVM";
    } else if (logtype.equals("5")) {
        cmd = resinStderrLogCmd;
        title = "Stderr";
    } else if (logtype.equals("6")) {
        cmd = resin3JVMLogCmd;
        title = "JVM";
    } else {
        //    自定义接受路径
        cmd = "tail -n "+showRow+" notes /weaver/ecology/" + logtype;
        title = "logtype";
        if (logtype.endsWith(".properties") || logtype.endsWith(".xml")) {
            encoding = "GBK";
        } else {
            encoding = "utf-8";
        }
    }
    String customerId = null2String(request.getParameter("customerId"));
    SvnListTempStatic svnListTempStatic = SvnListTempStatic.map.get(customerId);
    CustomInfo customInfo = svnListTempStatic.getCustomInfo();

    String vmIP = customInfo.getVmIP();
    String endpass = "";
    String[] pass = vmIP.split("\\.");
    for (String s : pass) {
        endpass += s;
    }
    endpass = "Test134##" + endpass;

    Connection conn = null;
    BufferedReader outBufferedReader = null;
    Session se = null;
    try {
        conn = new Connection(vmIP, 22);
        conn.connect(null, 1000, 0);

        boolean isAuthenticated = conn.authenticateWithPassword("test", endpass);
        if (isAuthenticated == false) {
            out.println("ssh登录服务器失败！");
            return;
        }
        se = conn.openSession();
        se.execCommand(cmd);

        InputStream is = new StreamGobbler(se.getStdout());
        String showlog = readMessage(is, encoding);
        if (showlog == null) {
            InputStream  iserr = new StreamGobbler(se.getStderr());
            showlog = readMessage(iserr, encoding);
        }
           showMessage=showlog;
    } catch (SocketTimeoutException e) {
           showMessage="ssh登录服务器超时！";
    } catch (Exception e) {
           showMessage=e.getMessage();
    } finally {

        if (outBufferedReader != null) {
            outBufferedReader.close();
        }
        if (se != null) {
            se.close();
        }
        if (conn != null) {
            conn.close();
        }
    }
%>
<html>
<head>
    <style>
        .fixpostion {
            cursor: pointer;
            position: fixed;
            margin-left: 90%;
            margin-top: 30%;
        }

        .circle {



            border-radius: 50%;
            width: 40px;
            height: 40px;
            background: #6b90f9;
        }

        .triangle {

            width: 0px;
            height: 0px;
            border-right: 20px solid transparent;
            border-left: 20px solid transparent;
            border-top: 20px solid #6b90f9;
        }
    </style>


</head>

<title><%=title%>
</title>
<body onload="window.scrollTo(0,document.body.scrollHeight);">
<div class="fixpostion" style="display: inline-block">
    <table>
        <tr>
            <td>
                <div class="circle" onclick="window.location.reload()"></div>
            </td>
            <td>
            <div class="triangle" onclick="window.scrollTo(0,document.body.scrollHeight);"></div>
            </td>
        </tr>
    </table>
</div>
<%=showMessage%>

</body>

<script type="application/javascript">
    // window.onload = function () {
    //     window.scrollTo(0, document.body.scrollHeight)
    // }
    window.onload = function () {
         window.scrollTo(0, document.body.scrollHeight)
     }
    document.onkeydown=function(event){
        var e = event || window.event || arguments.callee.caller.arguments[0];

        if(e && e.keyCode===40){ //下
            scrollDownPage()
        }
        if(e && e.keyCode==37){ //左
        }
        if(e && e.keyCode==39){ //右
        }
        if(e && e.keyCode==38){ // 上
            scrollUpPage()
        }
    };
    function scrollUpPage() {

        var pageHeight = document.body.clientHeight-50;
        let scrollTop = document.body.scrollTop;
        let stepLength = scrollTop - pageHeight;
        window.scrollTo(0,stepLength);
    }
    function scrollDownPage() {

        var pageHeight = document.body.clientHeight-50;
        let scrollTop = document.body.scrollTop;
        let stepLength = scrollTop + pageHeight;
        window.scrollTo(0,stepLength);
    }

</script>
</html>

